#!/usr/bin/env python
# coding=utf-8

if __name__ == '__main__':
    """ for file debug"""
    import sys,os
    sys.path.insert(0,os.path.join( os.getcwd(), '..' ))

import MySQLdb,time,threading
from vavava.base import LogAdapter

__author__ = 'vavava'

class DBConnector(object):
    def __init__(self,host="localhost",port="3306",user="root",
                 passwd="root",db="wr_trace",log=None):
        self.log= LogAdapter(log=log,mod_name="[wr_db_conn]")
        self.host=host
        self.port=port
        self.user=user
        self.passwd=passwd
        self.db=db
        self.conn=None
        self.cursor=None
        self.connected=False

    def get_cursor(self):
        """ return a db conn from db pool"""
        if self.conn is None:
            try:
                self.conn = MySQLdb.Connect(host=self.host,port=self.port,
                    user=self.user,passwd=self.passwd,db=self.db)
            except Exception as e:
                self.connected=False
                self.log.exception(e)
                return None
        self.connected=True
        self.cursor = self.conn.cursor()
        return self.cursor

